package com.king.mvpdemo.net;


import android.support.annotation.NonNull;
import android.util.Log;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Author King
 * * @Date 2019/8/5 10:00
 */
public class RetrofitClient {


    private static APIService apiService;

    private static final int API_TYPE_COMMON = 1; // 常用接口

    public static APIService getApiService() {
        if (apiService == null) {
            apiService = getApi(API_TYPE_COMMON);
        }
        return apiService;
    }

    private static APIService apiService1;

    private static final int API_TYPE_COMMON1 = 2; // 常用接口

    public static APIService getApiService1() {
        if (apiService1 == null) {
            apiService1 = getApi(API_TYPE_COMMON1);
        }
        return apiService1;
    }

    /**
     * 设置Header
     *
     * @return
     */
    private static Interceptor getHeaderInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder()
                        //添加Token
                        .header("token", "");
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        };

    }


    public static APIService getApi(int APIType) {
        //初始化一个client,不然retrofit会自己默认添加一个
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置Header
                .addInterceptor(getHeaderInterceptor())
                //设置拦截器
                .addInterceptor(new LoggingInterceptor())
                //响应时间
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .build();
        //获取请求地址
        String url = getAPIUrl(APIType);
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                //设置网络请求的Url地址
                .baseUrl(url)
                //设置数据解析器
                .addConverterFactory(GsonConverterFactory.create())
                //设置网络请求适配器，使其支持RxJava与RxAndroid
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        //创建—— 网络请求接口—— 实例
        return retrofit.create(APIService.class);
    }

    private static String getAPIUrl(int apiType) {
        String url = "";
        switch (apiType) {
            case API_TYPE_COMMON:
                url = "http://www.wanandroid.com/";
                break;
            case API_TYPE_COMMON1:
                url = UrlCommon.COMMON_API_URL;
                break;
        }
        return url;
    }
}
